﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slb.Ocean.Petrel.DomainObject;
using Public.Ocean.Petrel.Extensions.Services;
using Slb.Ocean.Petrel.UI;

namespace Public.Ocean.Petrel.Extensions.DomainObject
{
    public static class ProjectLinq
    {
        public static IEnumerable<TResult> Where<TResult>(this Project project, Func<TResult, bool> predicate) where TResult : class
        {
            return DomainObjectExtensions.PetrelServices(project).EnumerateRecursive<TResult>(predicate);
        }

        public static IEnumerable<TResult> Cast<TResult>(this Project project) where TResult : class
        {
            foreach (object obj in DomainObjectExtensions.PetrelServices(project).EnumerateRecursive())
            {
                TResult result = obj as TResult;
                if (result != null)
                {
                    yield return result;
                }
            }
        }

        public static IEnumerable<object> Where(this Project project, Func<object, bool> predicate)
        {
            return DomainObjectExtensions.PetrelServices(project).EnumerateRecursive<object>(predicate);
        }

        private static IEnumerable<ObjectServicePair<TObject, TService>> All<TObject, TService>(Project project, Func<TObject,TService> serviceFactory) where TObject : class
        {
            return from TObject obj in project select new ObjectServicePair<TObject, TService>(obj, serviceFactory(obj));
        }

        public static IEnumerable<ObjectServicePair<object, ISettingsInfo>> AllSettings(this Project project)
        {
            return from wrapper in DomainObjectExtensions.PetrelServices(project).EnumerateWrappersRecursive() 
                   select new ObjectServicePair<object, ISettingsInfo>(wrapper.Object, wrapper.Settings);
        }

        public static IEnumerable<ObjectServicePair<object, Statistics>> AllStatistics(this Project project)
        {
            return from wrapper in DomainObjectExtensions.PetrelServices(project).EnumerateWrappersRecursive()
                   select new ObjectServicePair<object, Statistics>(wrapper.Object, wrapper.Statistics);
        }

        public static IEnumerable<ObjectServicePair<object, IHistoryInfo>> AllHistory(this Project project)
        {
            return from wrapper in DomainObjectExtensions.PetrelServices(project).EnumerateWrappersRecursive()
                   select new ObjectServicePair<object, IHistoryInfo>(wrapper.Object, wrapper.History);
        }
    }
}